package org.robot.scm.platform.server.handler;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.robot.scm.platform.monitor.Clean3NoticeMonitor;
import org.robot.scm.platform.server.ScmHttpServer;
import org.robot.scm.pojos.enums.PlatformEnum;
import org.robot.scm.utils.RandomUtil;

/**
 * 清理三个月前的数据
 */
@Slf4j
@NoArgsConstructor
public class CleanUploadHandler extends BaseHandler implements HttpHandler {

    public static void main(String[] args) {
        // 保留1个月 天分区 p32,p33,p34,p35,p36,p37,p38,p39,p40,p41,p42,p43,p44,p45,p46,p47,p48,p49,p50,p51,p52,p53,p54,p55,p56,p57,p58,p59
        String clear1MonthDayPartition = Clean3NoticeMonitor.clearPartitionDay(2);
        // 保留1个月 月分区 p2
        String clear1MonthPartition = Clean3NoticeMonitor.clearPartitionMonth(2);
        // 保留1个月 按时间 2025-03-01 00:00:00
        String clear1MonthDate = Clean3NoticeMonitor.clearDate(1);

        // 保留2个月 月分区 p1
        String clear2MonthPartition = Clean3NoticeMonitor.clearPartitionMonth(3);
        // 保留2个月 按时间 2025-02-01 00:00:00
        String clear2MonthDate = Clean3NoticeMonitor.clearDate(2);
        // 保留2个月 天分区 p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,p16,p17,p18,p19,p20,p21,p22,p23,p24,p25,p26,p27,p28,p29,p30,p31
        String clear2MonthDayPartition = Clean3NoticeMonitor.clearPartitionDay(3);

        // 保留6个月 月分区 p9
        String clear6MonthPartition = Clean3NoticeMonitor.clearPartitionMonth(7);

        String clear6MonthDayPartition = Clean3NoticeMonitor.clearPartitionDay(7);


//        System.out.println(clear1MonthDayPartition);
//        System.out.println(clear1MonthPartition);
//        System.out.println(clear1MonthDate);
//        System.out.println(clear2MonthPartition);
//        System.out.println(clear2MonthDate);
//        System.out.println(clear2MonthDayPartition);
//        System.out.println(clear6MonthPartition);
        System.out.println(clear6MonthDayPartition);

    }

    @Override
    public String getUri() {
        return "/cleanUpload";
    }

    @Override
    public void handle(HttpExchange exchange) throws IOException {
        if (!exchange.getRequestMethod().equalsIgnoreCase("POST")) {
            sendFailureResponse(exchange, "请求非法");
            return;
        }

        String sqlSource = ScmHttpServer.getTargetConfig(PlatformEnum.CLEAN3).getCurrentPlatform().getNoticeSrc();
        String fileKey = RandomUtil.uuidNoLine();


        // 保留1个月 天分区 p32,p33,p34,p35,p36,p37,p38,p39,p40,p41,p42,p43,p44,p45,p46,p47,p48,p49,p50,p51,p52,p53,p54,p55,p56,p57,p58,p59
        String clear1MonthDayPartition = Clean3NoticeMonitor.clearPartitionDay(2);
        // 保留1个月 月分区 p2
        String clear1MonthPartition = Clean3NoticeMonitor.clearPartitionMonth(2);
        // 保留1个月 按时间 2025-03-01 00:00:00
        String clear1MonthDate = Clean3NoticeMonitor.clearDate(1);

        // 保留2个月 月分区 p1
        String clear2MonthPartition = Clean3NoticeMonitor.clearPartitionMonth(3);
        // 保留2个月 按时间 2025-02-01 00:00:00
        String clear2MonthDate = Clean3NoticeMonitor.clearDate(2);
        // 保留2个月 天分区 p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,p16,p17,p18,p19,p20,p21,p22,p23,p24,p25,p26,p27,p28,p29,p30,p31
        String clear2MonthDayPartition = Clean3NoticeMonitor.clearPartitionDay(3);

        // 保留6个月 月分区 p9
        String clear6MonthPartition = Clean3NoticeMonitor.clearPartitionMonth(7);

        // 保留6个月 天分区
        String clear6MonthDayPartition = Clean3NoticeMonitor.clearPartitionDay(7);

        // 替换sql中的占位符${cleanDate}为 2024-01-01 00:00:00 且记录行信息
        File targetFile = new File("assets/telegram/" + fileKey + ".sql");
        try (OutputStream out = Files.newOutputStream(targetFile.toPath()); BufferedReader reader = new BufferedReader(new InputStreamReader(Files.newInputStream(Paths.get(sqlSource)), StandardCharsets.UTF_8))) {
            String line;
            while ((line = reader.readLine()) != null) {
                if (StringUtils.isBlank(line)) {
                    continue;
                }
                // 替换清理日期、替换月分区、替换天分区、时间等
                String cleanedLine = line
                        .replaceAll("\\$\\{clear1MonthDayPartition\\}", clear1MonthDayPartition)
                        .replaceAll("\\$\\{clear1MonthPartition\\}", clear1MonthPartition)
                        .replaceAll("\\$\\{clear1MonthDate\\}", clear1MonthDate)
                        .replaceAll("\\$\\{clear2MonthPartition\\}", clear2MonthPartition)
                        .replaceAll("\\$\\{clear2MonthDate\\}", clear2MonthDate)
                        .replaceAll("\\$\\{clear2MonthDayPartition\\}", clear2MonthDayPartition)
                        .replaceAll("\\$\\{clear6MonthPartition\\}", clear6MonthPartition)
                        .replaceAll("\\$\\{clear6MonthDayPartition\\}", clear6MonthDayPartition);
                out.write(cleanedLine.concat("\n").getBytes(StandardCharsets.UTF_8));
            }
        } catch (IOException e) {
            sendFailureResponse(exchange, e.getMessage());
        }
        sendSuccessResponse(exchange, "上传成功", fileKey);
    }

}
